package com.mjk.manager.magicapi.core.core.servlet;

import com.mjk.manager.magicapi.core.core.context.MagicUser;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import java.util.function.Function;

public interface MagicRequestContextHolder {

    default <R> R convert(Function<ServletRequestAttributes, R> function) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes instanceof ServletRequestAttributes) {
            ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) requestAttributes);
            return function.apply(servletRequestAttributes);
        }
        return null;
    }

    MagicHttpServletRequest getRequest();

    MagicHttpServletResponse getResponse();

    /**
     * 设置用户信息
     *
     * @param user
     */
    public void setCurrentUser(MagicUser user);

    public MagicUser getCurrentUser();
}
